package ma.android.oook;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.ExpandableListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;
import android.widget.ExpandableListView.OnChildClickListener;

public class ListItems extends ExpandableListActivity{


	private OookDbAdapter mDbHelper;
	private ExpandableListAdapter mAdapter;
	private int [] itemsCodes;
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);

		mDbHelper = new OookDbAdapter(this);
		mDbHelper.open();

		ExpandableListView elv = getExpandableListView();
		elv.setOnChildClickListener(new OnChildClickListener(){

			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {
				// TODO Auto-generated method stub
				int selectedCode = itemsCodes[groupPosition];
				launchActivity(selectedCode);
				return false;
			}});
		Cursor items = null;
		Intent intent = getIntent();
		if (intent.getStringExtra(getString(R.string.operationType)).equals(getString(R.string.loans)))
		{
			items = mDbHelper.findAllLoans();
		}
		else if (intent.getStringExtra(getString(R.string.operationType)).equals(getString(R.string.borrowed)))
		{
			items = mDbHelper.findAllBorrowed();
		}
		if (items != null && items.getCount()>0)
		{
			int i = 0;
			startManagingCursor(items);
			List<Map<String, String>> primaryData = new ArrayList<Map<String, String>>();
			List<List<Map<String, String>>> secondaryData = new ArrayList<List<Map<String, String>>>();
			itemsCodes = new int[items.getCount()];
			while (!items.isLast())
			{
				items.moveToNext();
				itemsCodes[i]= items.getInt(items.getColumnIndex("id"));
				Map <String,String> m1 = new HashMap <String,String>();
				primaryData.add(m1);
				String type = items.getString(items.getColumnIndex("type"));
				long deadlineDate = items.getLong(items.getColumnIndex("deadline_date"));
				m1.put("type", type);
				Date d = new Date(deadlineDate);
				String ad = (d.getMonth()+1)+"/";
				ad = ad + d.getDate()+"/";
				ad = ad + (d.getYear()+1900);
				m1.put("deadlineDate",ad);

				List<Map<String, String>> children = new ArrayList<Map<String, String>>();
				Map <String,String> m2 = new HashMap <String,String>();
				children.add(m2);
				String contactName = items.getString(items.getColumnIndex("contact_name"));
				String description;
				if (items.getInt(items.getColumnIndex("amount")) == 0)
					description = items.getString(items.getColumnIndex("description"));
				else
					description = String.valueOf(items.getInt(items.getColumnIndex("amount")));
				m2.put("description", description);
				m2.put("contactName",String.valueOf(contactName));
				secondaryData.add(children);
				i++;
			}
			mAdapter = new SimpleExpandableListAdapter(
					this,
					primaryData,
					R.layout.expandablelistitem,
					new String[] { "type", "deadlineDate" },
					new int[] { R.id.listType, R.id.listDeadlineDate},
					secondaryData,
					R.layout.editlistitem,
					new String[] { "contactName" , "description"},
					new int[] { R.id.listContactName , R.id.listDescription }
			);
			setListAdapter(mAdapter);   
		}
	}
	private void launchActivity(int itemCod)
	{
		Intent intent = new Intent(this,NewItem.class);
		intent.putExtra(getString(R.string.itemCod), itemCod);
		intent.putExtra(getString(R.string.modeType), getString(R.string.editMode));
		if (getIntent().getStringExtra(getString(R.string.operationType)).equals(getString(R.string.loans)))
		{
			intent.putExtra(getString(R.string.operationType), getString(R.string.loan));
		}
		else
		{
			intent.putExtra(getString(R.string.operationType),getString(R.string.borrow));
		}
		startActivityForResult(intent, 1);
	}
	protected void onActivityResult(int requestCode, int resultCode, Intent data) 
	{
		if (requestCode == resultCode)
		{
			setResult(2);
			finish();
		}
	}
}